home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / ERRFIX.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  2KB  |  53 lines

  1. /*
  2. ** ERRFIX.C - redirect stderr to some other file under MS-DOS
  3. **
  4. ** by Bob Jarvis
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <process.h>
  11.  
  12. char *usage = "ERRFIX [filename] [prog] { {parm1} {parm2} ... {parmN} }\n"
  13.               "   Redirects stderr to another file, then invokes a program\n"
  14.               "   which will inherit the new definition of stderr.\n\n"
  15.               "Parameters:\n"
  16.               "   filename (required) - the name of the file stderr should\n"
  17.               "      be redirected to.  Output written to stderr will\n"
  18.               "      be routed to this file instead of the console.\n"
  19.               "   prog (required) - name of the program to be run.\n"
  20.               "   parm1...parmN (optional) - command-line parameters needed\n"
  21.               "      to run the program specified by the 'prog' argument.";
  22.  
  23. int main(int argc, char *argv[])
  24. {
  25.       char **args = argv;
  26.  
  27.       if (3 > argc)
  28.       {
  29.             printf(usage);
  30.             return 1;
  31.       }
  32.  
  33.       if (NULL != argv[argc]) /* may be a problem under some compilers */
  34.       {
  35.             args = malloc((argc+1) * sizeof(char *));
  36.             if (NULL == args)
  37.             {
  38.                   printf("Unable to allocate storage");
  39.                   return 2;
  40.             }
  41.  
  42.             memcpy(args, argv, argc * sizeof(char *));
  43.  
  44.             args[argc] = NULL;
  45.       }
  46.  
  47.       freopen(args[1], "w", stderr);
  48.  
  49.       spawnvp(0, args[2], &args[2]);
  50.  
  51.       return 0;
  52. }
  53.